package com.hongxun.design.utils.soundUtil;

import android.app.Activity;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.io.File;

import com.hongxun.design.R;
import com.hongxun.design.activitys.CreateProjectActivity;
import com.hongxun.design.adapters.PEnvironmentalAdapter;
import com.hongxun.design.bean.PhotoEnvironmentalItemBean;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.voicerecorder.VoiceRecorder;

/**
 * Created by zerdoor_pc on 2016/5/14.
 */
public class VoiceUtil  {
    Activity activity;
    public VoiceUtil(Activity activity,PEnvironmentalAdapter madapter){
        this.activity=activity;
        this.madapter=madapter;
        findViewOfVoice();
        setHandler();
    }

    PEnvironmentalAdapter madapter;
    private LinearLayout voice_rcd_hint_loading,
            voice_rcd_hint_tooshort;
    private RelativeLayout voice_rcd_hint_rcding;
    private View rcChat_popup;
    VoiceRecorder voiceRecorder;
    private LinearLayout del_re, my_voice;
    private ImageView volume;
    Handler handler;
    public static boolean btn_vocie = false;
    private int flag = 1;
    private long startVoiceT, endVoiceT;
    private boolean isShosrt = false;
    private String openid;
    private int[] micImages = new int[]{R.mipmap.amp1,
            R.mipmap.amp2,
            R.mipmap.amp3,
            R.mipmap.amp4,
            R.mipmap.amp5,
            R.mipmap.amp6,
            R.mipmap.amp7,
    };
    private void findViewOfVoice() {
        volume = (ImageView) activity.findViewById(R.id.volume);//音量
        voice_rcd_hint_rcding = (RelativeLayout) activity
                .findViewById(R.id.voice_rcd_hint_rcding);
        voice_rcd_hint_loading = (LinearLayout) activity
                .findViewById(R.id.voice_rcd_hint_loading);
        voice_rcd_hint_tooshort = (LinearLayout) activity
                .findViewById(R.id.voice_rcd_hint_tooshort);
        rcChat_popup = activity.findViewById(R.id.rcChat_popup);
        del_re = (LinearLayout) activity.findViewById(R.id.del_re);//取消
        my_voice = (LinearLayout) activity.findViewById(R.id.my_voice);//录制
        voiceRecorder = new VoiceRecorder(handler);
        openid = "";
    }

    private void setHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                volume.setImageResource(micImages[msg.what / 2]);
            }
        };
    }


    public boolean getOnTouchEvent(MotionEvent event){

        if (!Environment.getExternalStorageDirectory().exists()) {
            ToastShow.showShort(activity, "No SDCard");
            return false;
        }
        if (btn_vocie) {
            int[] location = new int[2];
            //if (view!=null)view.getLocationInWindow(location); // 获取在当前窗口内的绝对坐标
            int btn_rc_Y = location[1];
            int btn_rc_X = location[0];
            int[] del_location = new int[2];
            del_re.getLocationInWindow(del_location);
            int del_Y = del_location[1];
            int del_x = del_location[0];
            if (event.getAction() == MotionEvent.ACTION_DOWN && flag == 1) {
                if (!Environment.getExternalStorageDirectory().exists()) {
                    ToastShow.showShort(activity, "No SDCard");
                    return false;
                }
                if (event.getY() > btn_rc_Y && event.getX() > btn_rc_X) {
                    //判断手势按下的位置是否是语音录制按钮的范围内
                    //mBtnRcd.setBackgroundResource(R.drawable.radius_social_chat_voice);//语音录制开始
                    //mBtnRcd.setText("松开发送");
                    rcChat_popup.setVisibility(View.VISIBLE);
                    voice_rcd_hint_loading.setVisibility(View.VISIBLE);
                    voice_rcd_hint_rcding.setVisibility(View.GONE);
                    voice_rcd_hint_tooshort.setVisibility(View.GONE);
                    handler.postDelayed(new Runnable() {
                        public void run() {
                            if (!isShosrt) {
                                voice_rcd_hint_loading.setVisibility(View.GONE);
                                voice_rcd_hint_rcding
                                        .setVisibility(View.VISIBLE);
                            }
                        }
                    }, 300);
                    del_re.setVisibility(View.GONE);
                    my_voice.setVisibility(View.VISIBLE);
                    startVoiceT = System.currentTimeMillis();

                    try {
                        voiceRecorder.startRecording(null, openid, activity.getApplicationContext());
                    } catch (Exception e) {
                        if (voiceRecorder != null)
                            voiceRecorder.discardRecording();
                    }
                    flag = 2;
                }
            } else if (event.getAction() == MotionEvent.ACTION_UP && flag == 2) {//松开手势时执行录制完成
                //mBtnRcd.setBackgroundResource(R.drawable.radius_social_chat_voice_none);
                //mBtnRcd.setText("按住说话");
                if (del_re.getVisibility() == View.VISIBLE) {
                    rcChat_popup.setVisibility(View.GONE);
                    del_re.setVisibility(View.GONE);
                    my_voice.setVisibility(View.VISIBLE);
                    flag = 1;
                    if (voiceRecorder != null) {
                        File file = new File(voiceRecorder.getVoiceFilePath());
                        voiceRecorder.discardRecording();
                        if (file.exists()) {
                            file.delete();
                        }
                    }
                } else {
                    voice_rcd_hint_rcding.setVisibility(View.GONE);
                    endVoiceT = System.currentTimeMillis();// SystemClock.currentThreadTimeMillis();
                    flag = 1;
                    float time = (float) ((endVoiceT - startVoiceT) / 1000);
                    if (time < 1) {

                        //Log.i("", "环信===时间短");
                        isShosrt = true;
                        voice_rcd_hint_loading.setVisibility(View.GONE);
                        voice_rcd_hint_rcding.setVisibility(View.GONE);
                        voice_rcd_hint_tooshort.setVisibility(View.VISIBLE);
                        if (voiceRecorder != null) {
                            File file = new File(voiceRecorder.getVoiceFilePath());
                            voiceRecorder.discardRecording();
                            if (file.exists()) {
                                file.delete();
                            }
                        }
                        handler.postDelayed(new Runnable() {
                            public void run() {
                                voice_rcd_hint_tooshort
                                        .setVisibility(View.GONE);
                                rcChat_popup.setVisibility(View.GONE);

                                isShosrt = false;
                            }
                        }, 100);
                        //Log.i("", "环信===时间短返回");
                        return false;
                    }

                    int length = voiceRecorder.stopRecoding();
                    if (length > 0) {
                        //  updateVocie(voiceRecorder.getVoiceFilePath(), length, time);
                        if (madapter.speechHolder != null) {
                            PhotoEnvironmentalItemBean bean = madapter.getData().get(madapter.mPosition);
                            madapter.speechHolder.speech_button.setVisibility(View.GONE);
                            madapter.speechHolder.speech_play_button.setVisibility(View.VISIBLE);
//                            madapter.speechHolder.create_playtime.setText(time + "");
                            bean.setPath(voiceRecorder.getVoiceFilePath());
                            bean.setValue(time + "");
                            madapter.notifyDataSetChanged();
                        }
                    }
                }
            }
            if (event.getY() < btn_rc_Y) {//手势按下的位置不在语音录制按钮的范围内
                del_re.setVisibility(View.VISIBLE);
                my_voice.setVisibility(View.GONE);
                if (event.getY() >= del_Y
                        && event.getY() <= del_Y + del_re.getHeight()
                        && event.getX() >= del_x
                        && event.getX() <= del_x + del_re.getWidth()) {
                }

                if (voiceRecorder != null)
                    voiceRecorder.discardRecording();
            } else {
                del_re.setVisibility(View.GONE);
                my_voice.setVisibility(View.VISIBLE);

            }
        }
        return activity.onTouchEvent(event);
    }


}
